package com.mycar2w.cloud.web.controller;

import com.mycar2w.cloud.entity.TUser;
import com.mycar2w.cloud.service.pojo.TUserInfo;
import com.mycar2w.cloud.service.serviceimpl.UicService;
import com.mycar2w.cloud.web.model.UicRegisterModel;
import com.mycar2w.cloud.web.util.Md5Encrypt;
import com.mycar2w.cloud.web.util.ReturnModelAndView;
import com.umuw.cloud.core.util.JsonEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

import static com.mycar2w.cloud.web.util.ReturnModelAndView.returnFail;


/**
 * created time 2017/3/25
 *
 * @author ljc
 * @version V1.0
 */
@RestController
@RequestMapping("/uic")
public class UicController extends MycarBaseConTroller{
    @Autowired
    private UicService uicService;
    private TUser tUser;

    /**
     * 注册首页
     * @return 注册首页的视图 ModelAndView
     */
    @RequestMapping(value = "/register",method = RequestMethod.GET)
    public ModelAndView uicIndex(){
        return ReturnModelAndView.returnSuccess("register","heheh");
    }
    /**
     * 提交注册
     * @return 注册成功后的首页，即首页 ModelAndView
     */
    @RequestMapping(value = "/register_post",method = RequestMethod.POST)
    public ModelAndView uicRegistPost(@ModelAttribute UicRegisterModel uicRegisterModel){
        if(uicService.isRegister(uicRegisterModel.getPhone().trim())) {
           ModelAndView m= new ReturnModelAndView().returnFail("register","该手机号已经被注册了！请更换手机号注册！");
           return m;
        }
        if(uicService.isRegisterEmail(uicRegisterModel.getEmial().trim())) {
            return new ReturnModelAndView().returnFail("register","该邮箱已经被注册了！请更换邮箱号注册！");
        }
        uicRegisterModel.setPassword(Md5Encrypt.md5Encrypt(uicRegisterModel.getTuser().getPassword().trim()));
        uicService.register(uicRegisterModel.getTuser());
        httpSession.setAttribute("nickname",uicRegisterModel.getNickname());
        setSessionToken(uicRegisterModel.getPhone());
        System.out.println(httpSession.getAttribute("phone"));
        ModelAndView m=new ReturnModelAndView().returnSuccess("congratulation","恭喜你 注册成功");
        return m;

    }

    /**
     * 登陆页面
     * @return 登陆页面视图 ModelAndView
     */
    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public ModelAndView uicuserInfoLogin(){
        return ReturnModelAndView.returnSuccess("login");
    }
    /**
     * 提交登陆
     * @return 首页 ModelAndView
     */
    @RequestMapping(value = "/login_get",method = RequestMethod.POST)
    public ModelAndView uicUserInfoLoginGet(@RequestParam String phone,@RequestParam String password) throws Exception{
        if((tUser=uicService.login(phone.trim(),Md5Encrypt.md5Encrypt(password.trim())))!=null){
            httpSession.setAttribute("isMaster",tUser.getRole());
            httpSession.setAttribute("nickname",tUser.getNickname());
            setSessionToken(phone);
            return ReturnModelAndView.returnSuccess("index");
        }else {
            return returnFail("login","账号密码错误");
        }
    }

    /**
     * 修改密码页面
     * @return 修改密码页面 ModelAndView
     */
    @RequestMapping(value = "/password_change",method = RequestMethod.GET)
    public ModelAndView uicPasswordChange(){
        getMypPhone();
        return new ReturnModelAndView().returnSuccess("password_change");
    }
    /**
     * 修改密码
     * @return 首页
     */
    @RequestMapping(value = "/password_change_post",method = RequestMethod.POST)
    public ModelAndView uicPasswordChangePost(@Param("oldPassword") String oldPassword, @Param("newPasswordA") String newPasswordA,@Param("newPasswordB") String newPasswordB){

        String phone=getMypPhone();
        if(StringUtils.isBlank(oldPassword)||StringUtils.isBlank(newPasswordA)||StringUtils.isBlank(newPasswordB)){
            return ReturnModelAndView.returnFail("password_change","不能输入空");
        }
        if(!newPasswordA.equals(newPasswordB)){
            return ReturnModelAndView.returnFail("password_change","两次密码输入不一致");
        }
        if (!uicService.findByPhoneAndOldPasswore(phone,oldPassword)){
            return ReturnModelAndView.returnFail("password_change","原始密码错误");
        }
        uicService.changePassword(phone,newPasswordA);
        return ReturnModelAndView.returnSuccess("congratulation","修改密码成功");
    }

    /**
     * 个人信息页面
     * @return 个人信息页面 ModelAndView
     */
    @RequestMapping(value = "/detail",method = RequestMethod.GET)
    public ModelAndView uicDetailUpdate(){
        String phone=getMypPhone();
        TUserInfo tUserInfo=uicService.getDetail(phone);
        return ReturnModelAndView.returnSuccess("detail",tUserInfo);
    }

    /**
     * 修改个人信息
     * @return
     */
    @RequestMapping(value = "/detail_post",method = RequestMethod.POST)
    public ModelAndView uicDetailUpdatePost(@ModelAttribute TUserInfo tUserInfo) {
        String phone = getMypPhone();
        uicService.detailUpdate(phone, tUserInfo);
        return ReturnModelAndView.returnSuccess("c_info", "修改个人信息成功");
    }

    @RequestMapping(value = "/isLoad",method = RequestMethod.GET)
    public JsonEntity isLoad(){
        JsonEntity jsonEntity=new JsonEntity();
        if(httpSession.getAttribute("nickname")!=null){
            jsonEntity.setResult(7);
            Map<String,Object> map=new HashMap<>();
            map.put("isMaster",httpSession.getAttribute("isMaster"));
            map.put("nickname",httpSession.getAttribute("nickname"));
            jsonEntity.setData(map);
        }else {
            jsonEntity.setResult(8);
        }
        return jsonEntity;
    }
    @RequestMapping(value = "/zhuxiao")
    public ModelAndView cleanInfo(){
        getMypPhone();
        cleanLogin();
        return ReturnModelAndView.returnSuccess("index");
    }
    @RequestMapping(value = "/demoImg")
    public ModelAndView demoImg(){
        return ReturnModelAndView.returnSuccess("demoUp");
    }
}
